package cn.loan.controller;

import cn.loan.config.key.RequestKey;
import cn.loan.config.key.SessionKey;
import cn.loan.config.key.StatusCode;
import cn.loan.entity.User;
import cn.loan.service.ByStagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/5/12
 * @Time: 13:50
 * @Description: No Description
 */
@Controller
@RequestMapping("/quota")
public class QuotaController {

    @Autowired
    private ByStagesService byStagesService;
    @Autowired
    private HttpSession session;
    @Autowired
    private HttpServletRequest request;

    /**
     * 借款记录是否存在
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/loanExist")
    @ResponseBody
    public String quotaExist() throws Exception {
        final User user = (User) session.getAttribute(SessionKey.SESSION_USER);
        int userId = user.getUser_id();
        //是否有借款记录
        boolean loanExist = byStagesService.getLoanExist(userId);
        if (loanExist) {
            return StatusCode.STATUS_EXIST;
        } else {
            return StatusCode.STATUS_NOT_EXIST;
        }
    }


    /**
     * 跳转到无额度页面
     *
     * @return
     */
    @GetMapping("/no_quota")
    public String noQuota() {
        request.setAttribute(RequestKey.REQUEST_QUOTA, "0.00");
        return "/no_quota";
    }

}
